------------------------------------------------------------------------------------------------------------------------------------------------
-- -- tasks base lib
local function GetStringsTable(inst)
    return TUNING.NPC_CHAT_TABLE[ inst.npc_base_lib:Get_Language() ] ["npc_event_clockwork_guard"] or {}
end

local function OnAttached(inst,target) -- when the debuff on or player is newSpawn(or through the cave)
    inst.entity:SetParent(target.entity)
    inst.Network:SetClassifiedTarget(target)
    if TheWorld.ismastersim then
        local player = inst.entity:GetParent()
        if not player:HasTag("player") then
            inst:Remove()
            return
        end
        local pt = Vector3( player.Transform:GetWorldPosition() )

        local s_pt = player.npc_base_lib:GetSpawnPoint(pt,math.random(4,8)) or pt

        -- 
        SpawnPrefab("npc_fx_sky_door"):PushEvent("Set",{
            pt = Vector3(s_pt.x,s_pt.y+1,s_pt.z),
            scale = Vector3(2.5,1,2.5)
        })
        player:DoTaskInTime(3,function()
            local fishbowl = SpawnPrefab("npc_item_clockwork_guard_station_kit")
            fishbowl.Transform:SetPosition(s_pt.x,8,s_pt.z)
            player.npc_base_lib:Wisper(GetStringsTable(player).event_announce)     
        end)
        inst:Remove()
        return
    end
    inst:Remove()
end

local function OnDetached(inst) -- When eliminating from the outside
    local player = inst.entity:GetParent()   
    -- print(" FROM OUSIDE REMOVE ")
    
    -- inst:Remove()
end

local function ExtendDebuff(inst)
    -- inst.countdown = 3 + (inst._level:value() < CONTROL_LEVEL and EXTEND_TICKS or math.floor(TUNING.STALKER_MINDCONTROL_DURATION / FRAMES + .5))
end



local function OnUpdate(inst,ismastersim)
    -- local NPC = inst.entity:GetParent()
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddNetwork()

    inst:AddTag("CLASSIFIED")


    -- inst:DoPeriodicTask(1, OnUpdate, nil, TheWorld.ismastersim)  -- in one seconds
    inst:DoPeriodicTask( 0.5, OnUpdate, nil, TheWorld.ismastersim)  -- in 0.5 seconds

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("debuff")
    inst.components.debuff:SetAttachedFn(OnAttached)
    inst.components.debuff.keepondespawn = false -- 是否保持debuff 到下次登陆
    -- inst.components.debuff:SetDetachedFn(inst.Remove)
    inst.components.debuff:SetDetachedFn(OnDetached)
    -- inst.components.debuff:SetExtendedFn(ExtendDebuff)
    -- ExtendDebuff(inst)


    return inst
end

return Prefab("npc_event_clockwork_guard", fn)
